<?php
//require_once( FRAMEWORK_PATH . 'controllers/fb/FBConnector.php' );
class fbcontroller{

	private $fb_connector;

	public function __construct( $registry, $directCall=true )
	{
		$this->registry = $registry;

		$this->fb_connector = $this->registry->getObject('fbconnector');

		$urlBits = $this->registry->getObject('url')->getURLBits();
		$defaultProfileID = $registry->getObject('authenticate')->getUser()->getCurrentProfile()->getProfileID();
		$redirectURL = $this->registry->getSetting('redirecturl');
		
		switch ( isset( $urlBits[1] ) ? $urlBits[1] : '') {
			case 'connect':
				{
					$redirectURL = $redirectURL.'fb/create';
					$this->fb_connector->setRedirectURL($redirectURL);
					$permissons = 'email,read_stream,publish_stream,offline_access,user_photos,friends_photos,user_birthday';
					$this->fb_connector->connect($permissons, true);
				}
				break;
			case 'create':
				{
					if (false == isset($_SESSION['access_token']))
					{
						$redirectURL = $redirectURL.'fb/create';
						$this->fb_connector->setRedirectURL($redirectURL);
						$permissons = 'email,read_stream,publish_stream,offline_access,user_photos,friends_photos,user_birthday';
						$this->fb_connector->connect($permissons, true);
					}
					else
					{
						$this->fb_connector->ConnectwithToken($_SESSION['access_token'], true);
						$this->createProfile();
					}
				}
				break;
			default:
				{
					$url = $this->registry->getObject('url')->buildURL( array('profile', 'home', $user, $profileID), '', false );
					$this->registry->redirectUser( $url);
				}
				break;
		}
	}

	private function createProfile(){
	
		$user = $this->registry->getObject('authenticate')->getUser()->getUserID();
		$fbProfileID = $this->fb_connector->getUserID();
		$fbProfile = $this->fb_connector->getUserInfo($fbProfileID);
		
		$insert = array();
		$insert['user_id'] = $user;
		$include['about'] = $fbProfile[0]['about_me'];
		$insert['name'] = $fbProfile[0]['name'];
		$insert['email'] = $fbProfile[0]['email'];
		$insert['dob'] = $fbProfile[0]['birthday_date'];
		$insert['gender'] = $fbProfile[0]['sex'];
		//$insert['address'] = $fbProfile[0][];
		$profileName = 'Facebook-'.$fbProfile[0]['name'];
		$insert['profile_name'] = $profileName;
		$insert['employer'] = $fbProfile[0]['work'][0]['employer']['name'];
		$insert['college'] = $fbProfile[0]['education'][0]['school']['name'];
		$insert['profile_type'] = 2;
		$insert['access_token'] = $this->fb_connector->getAccesstoken();
		$insert['photo'] =  $fbProfile[0]['pic_square_with_logo'];

		$this->registry->getObject('db')->insertRecords( 'profile', $insert );

		$profileID = $this->registry->getObject('db')->lastInsertID();

		require_once( FRAMEWORK_PATH . 'models/profile.php' );
		$profile = new Profile( $this->registry, $user, $profileID);

		$url = $this->registry->getObject('url')->buildURL( array('profile', 'home', $user, $profileID), '', false );
		$this->registry->redirectUser( $url);
	}
}
?>

